﻿#region Copyright 2008 - Evolution Systems.
//
// Author: José Rogério Ferreira dos Santos(joserogerio.santos@gmail.com)
// Date: 02/01/2009
// Time: 15:08
// All rights are reserved. Reproduction or transmission in whole or in part, in
// any form or by any means, eletronic, mechanical or otherwise, is prohibited
// without the prior written permission of the author.
//
// Filename: ${FILE_NAME}.cs
#endregion
using System;
using System.Collections.Generic;

namespace csharp.rad.general
{
	/// <summary>
	/// Description of Phone.
	/// </summary>
	public class Phone : csharp.common.DefaultClass, csharp.common.IClass
	{
		#region private declarations
			private String _person_id;
			private String _type_id;
			private String _ddd;
			private String _number;
			private String _branch;
			private String _count; 
		#endregion
		
		public Phone()
		{
			clear();
			gsql.table = "common.phone";
			_sequence  = "common.phone_phone_id_seq";
		}
			
		#region properties
			public string personId
			{
				get { return  _person_id; }
				set { _person_id = value; }
			}
			
			
			public string typeId 
			{
				get { return _type_id; }
				set { _type_id = value; }
			}
			
			public string ddd 
			{
				get { return _ddd; }
				set { _ddd = value; }
			}
			
			public string number 
			{
				get { return _number; }
				set { _number = value; }
			}
			
			public string branch 
			{
				get { return  _branch; }
				set { _branch = value; }
			}
			
			public string count 
			{
				get { return _count; }
				set { _count = value; }
			}
		
		#endregion
		
		public void clear()
		{
			_id					= String.Empty;
			_person_id			= String.Empty;
			_type_id				= String.Empty;
			_ddd					= String.Empty;
			_number				= String.Empty;
			_branch				= Boolean.TrueString;
			_count				= String.Empty;
		}
		
		public override void get(System.Data.DataRow dr)
		{
			_id					= dr["phone_id"].ToString();
			_person_id			= dr["phone_person_id"].ToString();
			_type_id				= dr["phone_type_id"].ToString();
			_ddd				= dr["phone_ddd"].ToString();
			_number				= dr["phone_number"].ToString();
			_branch				= dr["phone_branch"].ToString();
			_count		= dr["phone_count"].ToString();			
		}
		
		public override void setGsql()
		{
			gsql.setValue(_id						, "phone_id");
			gsql.setValue(_person_id			, "phone_person_id");
			gsql.setValue(_type_id				, "phone_type_id");
			gsql.setValue(_ddd				, "phone_ddd");
			gsql.setValue(_number				, "phone_number");
			gsql.setValue(_branch				, "phone_branch");
			gsql.setValue(_count		, "phone_count");			
		}
		
 		public bool get(String personId, String count)
 		{
			gsql.prepare();
			gsql.setValue("","*");
			gsql.commandType = common.GenerateSql.TCommandType.CMD_SELECT;
			gsql.conditionalWhere= "phone_person_id = '"+personId+"' ";
			gsql.conditionalWhere= "phone_count = "+count;
			
			if (gsql.execute())
			{
				if (gsql.getRowCount() > 0)
				{
					get(gsql.getRow(0));
					return true;
				}
			}
			return false;	 			
 		}	
	}

	//List
	public class Phones : csharp.common.DefaultList, csharp.common.IList
	{		
		public Phones()
		{
			gsql.table= "common.phone";
		}
		
		
		public Phone get(Int32 index)
		{
			return (Phone)items[index];
		}
		
		public Phone get()
		{
			return (Phone)items[index];
		}
		
		public bool fillList()
		{
			gsql.prepare();
			gsql.setValue("","*");
			return mountList();
		}
		
		private bool mountList()
		{
			Int32 tam;
			
			if (!gsql.execute())
				return false;
			
			tam= gsql.getRowCount();
			clear();
			for (int i=0; i < tam; i++)
			{
				Phone p = new Phone();
				p.get(gsql.getRow(i));
				items.Add(p);
			}
			return true;
		}
	}   

	
}

